`
https://leetcode.cn/problems/maximum-number-of-occurrences-of-a-substring/
`

/**
 * @param {string} s
 * @param {number} maxLetters
 * @param {number} minSize
 * @param {number} maxSize
 * @return {number}
 */
var maxFreq = function (s, maxLetters, minSize, maxSize) {
  // 子串越短，包含的不同字母个数越少，越能满足 <= maxLetters 的要求
  // 子串越短，子串在 s 中的出现次数也越多
  // 所以只需考虑长度恰好等于 minSize 的子串即可

  const n = s.length
  const strCnt = new Map()
  const charCnt = new Map()
  let left = 0, right = 0

  // 维护一个长度为 minSize 的滑动窗口
  while (right < n) {
    const c = s[right++]
    charCnt.set(c, (charCnt.get(c) || 0) + 1)

    if (right - left >= minSize) {
      // 更新答案
      if (charCnt.size <= maxLetters) {
        const str = s.slice(left, right)
        strCnt.set(str, (strCnt.get(str) || 0) + 1)
      }

      const d = s[left++]
      const cnt = charCnt.get(d)
      if (cnt === 1) {
        charCnt.delete(d)
      } else {
        charCnt.set(d, cnt - 1)
      }
    }
  }

  return Math.max(0, ...strCnt.values())
};